/* * File: demoCounter.c * Author: douglas.armstead * * Created on April 29, 2015, 9:39 AM */ #include #include #define _XTAL_FREQ 1000000 #include #include "cortlandStd.h" void updatePins(int iL){ int pins[16];//An array of 16 integers for the 7-segment display. int temp; /* SUBLIME ASCII ART * a * ------- * | | * f| |b * | g | * ------- * | | * e| |c * | | * ------- * d * Three of the pins in PORTA and four in PORTB are used as the seven * outputs. The LED segments are assigned to the PORT bits as shown below: * PORTA PORTB * pins: 76543210 76543210 * segments: cde bagf * NOTE: 0 LIGHTS a segment and 1 makes it DARK * since the PIC sinks current from the LED display * Pin Binary Symbol Displayed */ pins[ 0] = 0b00000010; //0 pins[ 1] = 0b00110111; //1 pins[ 2] = 0b01000001; //2 pins[ 3] = 0b00010001; //3 pins[ 4] = 0b00110100; //4 pins[ 5] = 0b00011000; //5 pins[ 6] = 0b00001000; //6 pins[ 7] = 0b00110011; //7 pins[ 8] = 0b00000000; //8 pins[ 9] = 0b00110000; //9 pins[10] = 0b00100000; //A pins[11] = 0b00001100; //B pins[12] = 0b01001010; //C pins[13] = 0b00000101; //D pins[14] = 0b01001000; //E pins[15] = 0b01101000; //F // 0cdebagf //(correspondence between LED segments and pins[] bits) PORTA=pins[iL]>>4; //Use logic to retain the 4 most significant bits of PORTB and //replace the 4 least significant bits of PORTB with the 4 LSBs of pins[i]. temp=PORTB; PORTB=(temp|0b00001111)&(pins[iL]|0b11110000); return; } int main(int argc, char** argv) { OSCCONbits.IRCF=0b100; //This is what actually sets the frequency to 1MHz. ANSEL=0; int i; TRISA=0b00000000; //Set all RA pins as output. TRISB=0b01110000; //Set portB 4-6 as input, rest as outputs //RB4: reset, RB5:bump up, RB6: bump down. //Initialize the display to zero i=0; updatePins(i); PORTAbits.RA3=1; __delay_ms(150); PORTAbits.RA3=0; while(1){ if(PORTBbits.RB4==1){ i=0; updatePins(i); __delay_ms(150); } if(PORTBbits.RB5==1){ i+=1; if(i==16) i=0; updatePins(i); __delay_ms(150); } if(PORTBbits.RB6==1){ i-=1; if(i==-1) i=15; updatePins(i); __delay_ms(150); } } return (EXIT_SUCCESS); }